package com.example.za.account.activity;

import android.app.Activity;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.astuetz.PagerSlidingTabStrip;
import com.bigkoo.pickerview.TimePickerView;
import com.example.za.account.R;
import com.example.za.account.adapter.PagerAdapter;
import com.example.za.account.fragment.ExpenseFragment;
import com.example.za.account.fragment.IncomeFragment;
import com.example.za.account.model.Expense;
import com.example.za.account.model.Income;
import com.example.za.account.utils.Constant;
import com.example.za.account.utils.ScreenUtils;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class RecordActivity extends BaseActivity implements IncomeFragment.onTimePickListener,
        ExpenseFragment.onTimePickListener {

    @BindView(R.id.record_table_trip)
    PagerSlidingTabStrip recordTableTrip;
    @BindView(R.id.pager_record)
    ViewPager pagerRecord;
    private FragmentManager fragmentManager;
    private IncomeFragment incomeFragment;
    private ExpenseFragment expenseFragment;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_record);
        ButterKnife.bind(this);
        setTitle(getString(R.string.record_account));
        showBackwardView(true);
        fragmentManager = getSupportFragmentManager();
        Parcelable extra = getIntent().getParcelableExtra(Constant.RECORD);
        int index = 0;
        if (extra != null) {
            if (extra instanceof Income) {
                incomeFragment = IncomeFragment.getInstance((Income) extra);
                expenseFragment = new ExpenseFragment();
            } else if (extra instanceof Expense) {
                expenseFragment = ExpenseFragment.getInstance((Expense) extra);
                incomeFragment = new IncomeFragment();
                index = 1;
            }
        } else {
            expenseFragment = new ExpenseFragment();
            incomeFragment = new IncomeFragment();
        }
        List<Fragment> fragments = new ArrayList<>(2);
        fragments.add(incomeFragment);
        fragments.add(expenseFragment);
        pagerRecord.setAdapter(new PagerAdapter(fragmentManager, fragments));
        pagerRecord.setCurrentItem(index);
        recordTableTrip.setViewPager(pagerRecord);
        recordTableTrip.setTextSize(ScreenUtils.dp2sp(this, 16));
    }

    @Override
    protected Activity getSubActivity() {
        return this;
    }


    @Override
    public void DisplayDialog(Date date) {
        //时间选择器
        TimePickerView pvTime = new TimePickerView.Builder(this
                , new TimePickerView.OnTimeSelectListener() {

            @Override
            public void onTimeSelect(Date date, View v) {//选中事件回调

                if (pagerRecord.getCurrentItem() == 1) {
                    expenseFragment.setDate(date);
                } else {
                    incomeFragment.setDate(date);
                }
            }
        }).build();
        pvTime.setDate(Calendar.getInstance());//注：根据需求来决定是否使用该方法（一般是精确到秒的情况），此项可以在弹出选择器的时候重新设置当前时间，避免在初始化之后由于时间已经设定，导致选中时间与当前时间不匹配的问题。
        pvTime.show();
    }

}
